package com.example.neptune.startproject_109zj.model;

import com.example.neptune.startproject_109zj.bean.WeatherResult;
import com.example.neptune.startproject_109zj.iface.WeatherIface;
import com.example.neptune.startproject_109zj.iface.WeatherListener;
import com.example.neptune.startproject_109zj.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Neptune on 2017/3/14.
 */

public class WeatherModel implements WeatherIface{

    private Retrofit retrofit;
    private String BASEURL="http://www.weather.com.cn";
    private WeatherService weatherService;
    public WeatherModel() {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getWeather(String cityno, final WeatherListener Listener) {
       //Retrofit using3-2
        weatherService=retrofit.create(WeatherService.class);
        Call<WeatherResult>call=weatherService.getResult(cityno);

        //Retrofit using3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {

                if(response.isSuccessful() && response.body()!=null)
                    Listener.onResponse(response.body().getWeatherinfo());
                    else
                    Listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                Listener.onFail(t.toString());

            }
        });
    }
}
